package hut.game.unit;

import hut.game.GameActivity;
import hut.game.MainActivity;
import hut.game.R;
import hut.game.table.BanCo;
import hut.game.table.VanCo;
import android.util.Log;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

public class QuanCo {
	public int Hang;
	public int Cot;
	public String Ten;// Ten quan co {Tuong,Sy,Tinh,Xe,Phao,Ma,Chot}
	public int Phe;// Phe = 0 || Phe = 1 || Phe = 2 (Táº¡i Ă´ Ä‘Ă³ ko cĂ³ quĂ¢n cá»�)
	public String ThuTu = "";// VD: quan Ma phia ben Trai, quan Ma phia benp hai.
	public int TrangThai;// TrangThai = 0: QuanCo da bi an | Trangthai = 1: QuanCo van con song chua lat | TrangThai = 2: QuanCo da duoc lat len
	public String TenQuanChuaLat = "";
	public String ThuTuChuaLat = "";
	public boolean Khoa = false;
	public ImageView imgUnit;
	public boolean isFirst = true;
	
	public QuanCo() {
		this.isFirst = false;
		this.Hang = 10;
		this.Cot = 10;
		this.Ten = "";
		this.Phe = 2;
		this.ThuTu = "";
		this.TrangThai = 0;
		this.TenQuanChuaLat =  "";
		this.Khoa = true;
		this.imgUnit = new ImageView(GameActivity.gameContext);
	}
	
	public int getDiem(){
		return 0;
	}
	
	public void KhoiTaoListener(OnTouchListener t)
	{
		imgUnit.setOnTouchListener(t);
	}
	
	
	public void KhoiTao(int phe, String name, String thutu, int stt,boolean khoa, int x, int y) // Khoi tao quan co voi Phe=phe,Ten=name, TrangThai=stt, Khoa=khoa, Vi Tri=[x,y])
	{
		this.Hang = x;
		this.Cot = y;
		this.Ten = name;
		this.TrangThai = stt;
		this.Phe = phe;
		this.ThuTu = thutu;
		this.TenQuanChuaLat = name;
		this.ThuTuChuaLat = thutu;
		this.Khoa = khoa;
		this.isFirst = true;
		BanCo.ViTri[x][y].Hang = x;
		BanCo.ViTri[x][y].Cot = y;
		BanCo.ViTri[x][y].Ten = name;
		BanCo.ViTri[x][y].ThuTu = thutu;
		BanCo.ViTri[x][y].Phe = phe;
		BanCo.ViTri[x][y].Trong = false;
		// Ve quan Co  setOnClick
		this.draw();
	}
	
	public void KhoiTaoViTri(int x, int y){
		this.Hang = x;
		this.Cot = y;
		BanCo.ViTri[x][y].Hang = x;
		BanCo.ViTri[x][y].Cot = y;
		BanCo.ViTri[x][y].Ten = this.Ten;
		BanCo.ViTri[x][y].ThuTu = this.ThuTu;
		BanCo.ViTri[x][y].Phe = this.Phe;
		BanCo.ViTri[x][y].Trong = false;
		// Ve quan Co  setOnClick
		this.draw();
	}
	
	
	public void drawImage(){
		if (Phe == 0) {
			if(TrangThai == 2){
				if(VanCo.Marked){
					if (Ten == "tuong") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "sy") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "tinh") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "xe") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "phao") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "ma") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "chot") imgUnit.setImageResource(R.drawable.hidden_marked);
				}else{
					if (Ten == "tuong") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "sy") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "tinh") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "xe") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "phao") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "ma") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "chot") imgUnit.setImageResource(R.drawable.hidden);
				}
				
			}
			if(TrangThai == 1){
				if(VanCo.Marked){
					if (Ten == "tuong") imgUnit.setImageResource(R.drawable.rtuong_marked);
					if (Ten == "sy") imgUnit.setImageResource(R.drawable.rsy_marked);
					if (Ten == "tinh") imgUnit.setImageResource(R.drawable.rtinh_marked);
					if (Ten == "xe") imgUnit.setImageResource(R.drawable.rxe_marked);
					if (Ten == "phao") imgUnit.setImageResource(R.drawable.rphao_marked);
					if (Ten == "ma") imgUnit.setImageResource(R.drawable.rma_marked);
					if (Ten == "chot") imgUnit.setImageResource(R.drawable.rchot_marked);
				}else{
					if (Ten == "tuong") imgUnit.setImageResource(R.drawable.rtuong);
					if (Ten == "sy") imgUnit.setImageResource(R.drawable.rsy);
					if (Ten == "tinh") imgUnit.setImageResource(R.drawable.rtinh);
					if (Ten == "xe") imgUnit.setImageResource(R.drawable.rxe);
					if (Ten == "phao") imgUnit.setImageResource(R.drawable.rphao);
					if (Ten == "ma") imgUnit.setImageResource(R.drawable.rma);
					if (Ten == "chot") imgUnit.setImageResource(R.drawable.rchot);
				}
				
			}
		}
		if (Phe == 1) {
			if(TrangThai == 2){
				if(VanCo.Marked){
					if (Ten == "tuong") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "sy") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "tinh") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "xe") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "phao") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "ma") imgUnit.setImageResource(R.drawable.hidden_marked);
					if (Ten == "chot") imgUnit.setImageResource(R.drawable.hidden_marked);
				}else{
					if (Ten == "tuong") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "sy") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "tinh") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "xe") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "phao") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "ma") imgUnit.setImageResource(R.drawable.hidden);
					if (Ten == "chot") imgUnit.setImageResource(R.drawable.hidden);
				}
			}
			if(TrangThai == 1){
				if(VanCo.Marked){
					if (Ten == "tuong")	imgUnit.setImageResource(R.drawable.btuong_marked);
					if (Ten == "sy") imgUnit.setImageResource(R.drawable.bsy_marked);
					if (Ten == "tinh")imgUnit.setImageResource(R.drawable.btinh_marked);
					if (Ten == "xe")imgUnit.setImageResource(R.drawable.bxe_marked);
					if (Ten == "phao")imgUnit.setImageResource(R.drawable.bphao_marked);
					if (Ten == "ma")imgUnit.setImageResource(R.drawable.bma_marked);
					if (Ten == "chot")imgUnit.setImageResource(R.drawable.bchot_marked);
				}else{
					if (Ten == "tuong")	imgUnit.setImageResource(R.drawable.btuong);
					if (Ten == "sy") imgUnit.setImageResource(R.drawable.bsy);
					if (Ten == "tinh")imgUnit.setImageResource(R.drawable.btinh);
					if (Ten == "xe")imgUnit.setImageResource(R.drawable.bxe);
					if (Ten == "phao")imgUnit.setImageResource(R.drawable.bphao);
					if (Ten == "ma")imgUnit.setImageResource(R.drawable.bma);
					if (Ten == "chot")imgUnit.setImageResource(R.drawable.bchot);
				}
			}
		}
	}
	// ve quan co vao vi tri tren ban co
	public void draw() {
		drawImage();
		// Váº½ quĂ¢n cá»�
		imgUnit.setLayoutParams(new LayoutParams((int)(30 * MainActivity.mScale), (int)(30 * MainActivity.mScale)));
		GameActivity.setLayout(imgUnit, Cot * 51 + 10  , Hang * 70  + 98, 0, 0);
		// Thiáº¿t láº­p quĂ¢n cá»� trĂªn BĂ n Cá»�
		BanCo.ViTri[Hang][Cot].Hang = Hang;
		BanCo.ViTri[Hang][Cot].Cot = Cot;
		BanCo.ViTri[Hang][Cot].Trong = false;
		BanCo.ViTri[Hang][Cot].Ten = Ten;
		BanCo.ViTri[Hang][Cot].ThuTu = ThuTu;
		BanCo.ViTri[Hang][Cot].Phe = Phe;
	}

	public void imgUnitClick(View v) {
		BanCo.ResetCanMove();
		if(this.Khoa == false){
			if (VanCo.Marked) {
				if (this.TrangThai != 0) {
					if (this.Phe == VanCo.DanhDau.Phe) {
						VanCo.Marked = false;
						if (VanCo.DanhDau.Phe == 0) {
							VanCo.DanhDau.drawImage();
						}
						if (VanCo.DanhDau.Phe == 1) {
							VanCo.DanhDau.drawImage();
						}
						BanCo.ResetCanMove();
					}
					if (this.Phe != VanCo.DanhDau.Phe) {
						if (VanCo.DanhDau.KiemTra(this.Hang, this.Cot) == 1	&& VanCo.DanhDau.TuongAnToan(this.Hang, this.Cot) == 1)// Náº¿u nÆ°á»›c Ä‘i há»£p lá»‡
						{
							// Ghi thong tin nuoc di vao game log
//							VanCo.LuuVaoGameLog(v, this);
							// An quan co cua doi phuong
							VanCo.AnQuanCo(this);
							// Tra lai o co trong
							VanCo.OCoTrong(VanCo.DanhDau.Hang, VanCo.DanhDau.Cot);
							// Thiet lap quan co tren ban co
							VanCo.DatQuanCo(this, VanCo.DanhDau, this.Hang,this.Cot);
							// Thay doi luot di
							VanCo.DoiLuotDi();
							VanCo.KiemTraChieuBi();
							if (VanCo.winner != 2)
							{
								if(VanCo.state)
								{
									if (VanCo.winner == 0) GameActivity.ThongBaoKetThuc.KhoiTao("Player 1 has winner", 1);
									if (VanCo.winner == 1) GameActivity.ThongBaoKetThuc.KhoiTao("Player 2 has winner", 1);
								}else
								{
									if (VanCo.winner == 0) GameActivity.ThongBaoKetThuc.KhoiTao("Computer has winner", 1);
									if (VanCo.winner == 1) GameActivity.ThongBaoKetThuc.KhoiTao("Player 1 has winner", 1);
								}
								GameActivity.showResult();
								BanCo.KhoaBanCo();
							}
							// kiem tra chieu tuong
							VanCo.KiemTraChieuTuong();
							BanCo.ResetCanMove();
						}
						VanCo.Marked = false;

						if (VanCo.DanhDau.Phe == 0) {
							VanCo.DanhDau.drawImage();
						}
						if (VanCo.DanhDau.Phe == 1) {
							VanCo.DanhDau.drawImage();
						}
						BanCo.ResetCanMove();
					}
				}
			} else {
				if (this.TrangThai != 0) {
					if (this.Khoa == false) {
						// Danh dau quan co duoc chon
						VanCo.Marked = true;
						VanCo.DanhDau = new QuanCo();
						VanCo.DanhDau = this;

						drawImage();
						for (int i = 0; i <= 9; i++) {
							for (int j = 0; j <= 8; j++) {
								if (i != Hang || j != Cot)
								{
									Log.d("tuong", this.Ten + "/" + v.getId() + "/" + VanCo.DanhDau.KiemTra(i, j) + "/" + VanCo.DanhDau.TuongAnToan(i, j));
									if (VanCo.DanhDau.KiemTra(i, j) == 1	&& VanCo.DanhDau.TuongAnToan(i, j) == 1)
									{
										BanCo.ViTri[i][j].imgMove.setVisibility(ImageView.VISIBLE);
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	public void imgUnitChuaLatClick(View v,QuanCo qcl){
		BanCo.ResetCanMove();
		if(this.Khoa == false){
			if (VanCo.Marked) {
				if (this.TrangThai != 0) {
					if (this.Phe == VanCo.DanhDau.Phe) {
						VanCo.Marked = false;
						if (VanCo.DanhDau.Phe == 0) {
							VanCo.DanhDau.drawImage();
						}
						if (VanCo.DanhDau.Phe == 1) {
							VanCo.DanhDau.drawImage();
						}
						BanCo.ResetCanMove();
					}
					if (this.Phe != VanCo.DanhDau.Phe) {
						if (VanCo.DanhDau.KiemTra(this.Hang, this.Cot) == 1	&& VanCo.DanhDau.TuongAnToan(this.Hang, this.Cot) == 1)// Náº¿u nÆ°á»›c Ä‘i há»£p lá»‡
						{
							// Ghi thong tin nuoc di vao game log
//							VanCo.LuuVaoGameLog(v, this);
							// An quan co cua doi phuong
							VanCo.AnQuanCo(this);
							// Tra lai o co trong
							VanCo.OCoTrong(VanCo.DanhDau.Hang, VanCo.DanhDau.Cot);
							// Thiet lap quan co tren ban co
							VanCo.DatQuanCo(this, VanCo.DanhDau, this.Hang,this.Cot);
							// Thay doi luot di
							VanCo.DoiLuotDi();
							VanCo.KiemTraChieuBi();
							if (VanCo.winner != 2)
							{
								if(VanCo.state)
								{
									if (VanCo.winner == 0) GameActivity.ThongBaoKetThuc.KhoiTao("Player 1 has winner", 1);
									if (VanCo.winner == 1) GameActivity.ThongBaoKetThuc.KhoiTao("Player 2 has winner", 1);
								}else
								{
									if (VanCo.winner == 0) GameActivity.ThongBaoKetThuc.KhoiTao("Computer has winner", 1);
									if (VanCo.winner == 1) GameActivity.ThongBaoKetThuc.KhoiTao("Player 1 has winner", 1);
								}
								GameActivity.showResult();
								BanCo.KhoaBanCo();
							}
							// kiem tra chieu tuong
							VanCo.KiemTraChieuTuong();
							BanCo.ResetCanMove();
						}
						VanCo.Marked = false;

						if (VanCo.DanhDau.Phe == 0) {
							VanCo.DanhDau.drawImage();
						}
						if (VanCo.DanhDau.Phe == 1) {
							VanCo.DanhDau.drawImage();
						}
						BanCo.ResetCanMove();
					}
				}
			} else {
				if (this.TrangThai != 0) {
					if (this.Khoa == false) {
						// Danh dau quan co duoc chon
						VanCo.Marked = true;
						VanCo.DanhDau = new QuanCo();
						VanCo.DanhDau = this;

						drawImage();
						for (int i = 0; i <= 9; i++) {
							for (int j = 0; j <= 8; j++) {
								if (i != this.Hang || j != this.Cot)
								{
									if (qcl.KiemTra(i, j) == 1	&& qcl.TuongAnToan(i, j) == 1)
									{
										BanCo.ViTri[i][j].imgMove.setVisibility(ImageView.VISIBLE);
									}
								}
							}
						}
					}
				}
			}
		}
	}

	public int KiemTra(int i, int j) {
		return 0;
	}

	public int TuongAnToan(int i, int j) {
		return 0;
	}
}
